<template>
  <div class="progress_container">
    <timeline :data="progressList"></timeline>
  </div>
</template>

<script>
import timeline from '@/components/timeline'
import { getBuyerApplyInfo } from '@/api'
import { EncryptSM4 } from '@/utils/allSM'
export default {
  components: {
    timeline
  },
  data() {
    return {
      progressList: [],
      // 页面加载状态
      toast: null,
    }
  },
  created() {
    this.toast = this.$zgtToast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true,
      message: '加载中',
    });
    this.getInfo()
  },
  methods: {
    getInfo() {
      let params = {
        id: this.$route.params.id,
        uId: this.$route.params.uid
      }
      console.log('进度传参', params);
      getBuyerApplyInfo({ jsons: EncryptSM4(JSON.stringify(params)) })
        .then(res => {
          let stateList = this.$store.state.stateList

          let list = stateList.map(item => {
            let findItem = res.data.find(i => i.state == item.state)
            if (findItem) {
              item = Object.assign(item, findItem)
            } else {
              item.optTime = ''
            }
            return item
          })

          // 处理作废、冻结 插入一条状态
          if (this.$route.params.state == 0 || this.$route.params.state == 91) {
            let obj = {
              optTime: this.$route.params.optTime,
              stateName: this.$route.params.stateName,
              state: this.$route.params.state
            }
            let idx = null
            for (let i = 0; i < list.length; i++) {
              const item = list[i];
              if (!item.optTime) {
                idx = i
                break
              }
            }

            if (typeof idx === 'number') {
              list.splice(idx, 0, obj)
            }
          }

          // 处理作废后续流程不显示
          if (this.$route.params.state == 0) {
            list = list.filter(item => {
              if (item.optTime) {
                return item
              }
            }).sort((a, b) => {
              return a.sort - b.sort
            })
          }


          this.progressList = list
          this.toast.clear()
        })
        .catch(err => {
          console.log('进度查看err', err);
          this.toast.clear()
        })
    }
  }
}
</script>


<style lang="less" scoped>
.progress_container {
  height: 100%;
  background-color: #F4F7FC;
  flex: 1;
}
</style>